/**
 * 
 */
package cn.ac.istic.lkt.atv;

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

/**
 * @author scd
 *
 */
public class Convert2Image {
	int totallen = 0;
	int threadno = 1;
	public class ConvertThread extends Thread{
		List<String> lines = null;
		int start=0, end=0;
		String dir = null;
		String prefix = null;
		public ConvertThread(List<String> ls,int a, int b,String d, String p){
			lines = ls;
			start=a;
			end=b;
			dir = d;
			prefix=p;
		}
		public void run(){
			System.out.println("thead "+start+" "+end+" started.");
			
			TreePanel tp = new TreePanel();			
			for (int i=start; i<end; i++){
				ParseTree pt = new ParseTree(lines.get(i));
				int numnum= (""+i).length();
				StringBuffer sb = new StringBuffer();
				for (int j =0; j < (totallen-numnum); j++){
					sb.append("0");
				}
				sb.append(""+i);			
				
				tp.init(pt);
				BufferedImage bi = tp.paintImage();
				try {			    
				    File outputfile = new File(dir+"/"+prefix+sb+".png");
				    ImageIO.write(bi, "png", outputfile);
				} catch (IOException e) {
				    e.printStackTrace();
				}

			}
		}
		
	}

	public Convert2Image(String[] args){
		File f = new File(args[0]);
		if (f==null || !f.canRead()) {
			System.err.println("Can't read file: "+args[1]);
			System.exit(-1);
		}
		if (! new File(args[1]).isDirectory()){
			System.err.println("Not a directory: "+args[1]);
			System.exit(-1);
		}
		if (args.length==4){
			threadno = Integer.parseInt(args[3]);
			if(threadno<=0){
				System.err.println("Thread number set incorrect, use default 1.");
				threadno = 1;
			}
		}
		
		InputStream in = null;
		List<String> lines = new ArrayList<String>();
		try {
			in = new FileInputStream(f);
		    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		    String line = null;
		    while ((line = reader.readLine()) != null) {
		    	if(line.trim().length()==0){
		    		continue;
		    	}
		        lines.add(line.trim());
		    }		    
		} catch (IOException x) {
		    System.err.println(x);		   
		} 
		
		int total=lines.size();
		totallen=(""+total).length();
		int part = total/threadno+1;
		//System.out.println(totallen);
		int s=0,e=part;
		
		//ConvertThread ct = new ConvertThread(ptlist,s,e,args[1],args[2]);
		//ct.start();
		while (true){		
			ConvertThread ct1 = new ConvertThread(lines,s,e,args[1],args[2]);
			ct1.start();

			s=e;
			e=s+part;
			if(e>total){
				e=total;
			}
			if(s>=total){
				break;
			}
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//System.err.println(args.length);
		if(args.length < 3){
			System.err.println("usage: java cn.ac.istic.lkt.atv.Convert2Image input-file-name output-directory output-prefix [thread-num]");
			System.exit(-1);
		}
		// TODO Auto-generated method stub

		
		new Convert2Image(args);
		

	}

}
